# ------------------------------
# 3. Pydantic模型 (数据验证和序列化)
#    就业表(Emp)相关模型
# ------------------------------
from datetime import date
from typing import Literal, Annotated, Optional

from pydantic import BaseModel, Field, AfterValidator
from sqlalchemy import Enum

from day20.schema.schema_stu import StuSchema


# 定义学历枚举类型
class EducationLevel(str, Enum):
    JUNIOR_COLLEGE = "大专"
    BACHELOR = "本科"
    MASTER = "硕士"
    DOCTOR = "博士"

# OFFER时间验证器：确保OFFER下发时间不早于就业开放时间
def check_offer_time(v: date, values) -> date:
    if v <= values.data.get('emp_open_time'):
        raise ValueError("OFFER下发时间不能早于就业开放时间")
    return v

class EmpSchema(BaseModel):
    """就业表基础模型，包含公共字段"""
    stu_id: int = Field(..., gt=0, description="学生编号（外键关联stu表）")
    # 学生姓名：1-20个字符
    stu_name: Optional[str] = Field(None, min_length=1, max_length=20, description="学生姓名")
    # 学生班级：字符串，1-20个字符
    class_name: Optional[str] = Field(None, min_length=1, max_length=20, description="学生班级")
    emp_open_time: Optional[date] = Field(None, description="就业开放时间")
    offer_issue_time: Optional[Annotated[
        date,
        Field(description="OFFER下发时间"),
        AfterValidator(check_offer_time)
    ]] = None
    emp_company_name: Optional[str] = Field(None, description="就业公司名称")
    emp_salary: Optional[int] = Field(None, ge=0, description="就业薪资（非负数）")

class EmpCreateScheme(EmpSchema):
    """创建就业信息模型：外键stu_id必填"""
    pass

class EmpUpdateScheme(BaseModel):
    """更新就业信息模型：所有字段均为可选"""
    emp_open_time: Optional[date] = Field(None, description="就业开放时间")
    # 学生姓名：1-20个字符
    stu_name: Optional[str] = Field(None, min_length=1, max_length=20, description="学生姓名")
    # 学生班级：字符串，1-20个字符
    class_name: Optional[str] = Field(None, min_length=1, max_length=20, description="学生班级")

    # offer_issue_time: Optional[date] = Field(None, description="就业开放时间")
    offer_issue_time: Optional[Annotated[
        date,
        Field(description="OFFER下发时间"),
        AfterValidator(check_offer_time)
    ]] = None
    emp_company_name: Optional[str] = Field(None, description="就业公司名称")
    emp_salary: Optional[int] = Field(None, ge=0, description="就业薪资（非负数）")


class EmpResponseSchema(EmpSchema):
    """就业信息响应模型：包含自增主键和关联的学生信息"""
    id: int = Field(..., description="自增序号（主键）")
    # to_stu: Optional[StuSchema] = None  # 关联的学生基础信息

    class Config:
        from_attributes = True


# EmpResponseSchema.update_forward_refs()